package com.liangzhong.model.playerCenter
{
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.goods.vo.PlayerStarVo;
	import com.liangzhong.model.playerCenter.vo.BattlearrayVo;
	import com.liangzhong.model.playerCenter.vo.PlayerCenterInfoVo;
	import com.liangzhong.model.playerCenter.vo.PlayerPlaceVo;
	import com.liangzhong.model.playerCenter.vo.PlayerVo;
	import com.liangzhong.model.playerCenter.vo.TrainingTacticsVo;
	import com.liangzhong.proxyconstant.PlayerCenter.PlayerCenterConstant;
	
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;

	/**
	 *球员中心 
	 * @author Administrator
	 * 
	 */	
	public class PlayerCenterDataManager
	{
		/**
		 *受伤球员数量5（超过数量不可进行比赛） 
		 */
		public static const INJURED:int = 5;
		
		/**
		 *球员数据 
		 */
		private var _playerList:Array;
		private var _playerCenterVo:PlayerCenterInfoVo;
		private var _firstBattlearrayVo:BattlearrayVo;
		private var _secondBattlearrayVo:BattlearrayVo;
		/**
		 * 阵型训练坐标数据 
		 */
		private var _tacticsDic:Dictionary;
		public function PlayerCenterDataManager()
		{
		}
		/**
		 *获得球员列表数据 
		 * @param $obj
		 * 
		 */
		public function getPlayerList($obj:Object):void
		{
			_playerList = [];
			if(_playerCenterVo == null)
				_playerCenterVo = new PlayerCenterInfoVo();
			_playerCenterVo.analysisData($obj.playcentre);
			var arr:Array = $obj.player;
			var len:int = arr.length;
			var vo:PlayerVo;
			for(var i:int = 0;i < len;i++)
			{
				vo = new PlayerVo();
				vo.analysisData(arr[i]);
				_playerList.push(vo);
//				trace("球员列表中的球员ID" + vo.starid);
			}
			_playerCenterVo.playersNumber = len;
			_playerCenterVo.updataphysicalTime =  AppData.getInstance().clientHourse;
//			testArr(_playerList);
		}
//		private function testArr($arr:Array):void
//		{
//			var arr:Array = $arr.concat();
//			arr.splice(0,arr.length);
//		}
		/**
		 *获得阵容数据 
		 * @param $obj
		 * 
		 */
		public function getBattlearrayInfo($obj:Object):void
		{
			if(_firstBattlearrayVo != null)return;//如果有每一阵容的数据，不在设置
			_firstBattlearrayVo = new BattlearrayVo();
			_secondBattlearrayVo = new BattlearrayVo();
			var arr:Array = $obj.playformation ;
			var len:int = arr.length;
			var vo:PlayerPlaceVo;
			for(var i:int = 0;i < len;i++)
			{
				vo = new PlayerPlaceVo();
				vo.analysisData(arr[i]);
//				trace("阵容中的球员ID" + vo.playerid);
				if(vo.formation == PlayerCenterDataConstant.FIRSTBATTLEARRAY)
				{
					_firstBattlearrayVo.addPlaceVo(vo);
				}else
				{
					_secondBattlearrayVo.addPlaceVo(vo);
				}
			}
		}
		/**
		 * 根据球员ID获取球员数据 
		 * @param $id
		 * @return 
		 * 
		 */
		public function getplayerVoById($id:int):PlayerVo
		{
			for each(var goods :PlayerVo in _playerList)
			{
				if(goods.playerid == $id)return goods;
			}
			return null;
		}
		/**
		 *增加一个球员  
		 * {playerId:playerId,starId:starId}
		 */
		public function addPlayerVo($obj:Object):void
		{
			var playerVo:PlayerVo = new PlayerVo();
			_playerList.push(playerVo);
			playerVo.playerid = $obj.playerId;
			playerVo.addPlayer($obj.starId);
			playerVo.starid = $obj.starId;
			playerVo.ability = $obj.ability;
			playerVo.number = $obj.number;
			playerVo.nextexp = $obj.nextexp;
			_playerCenterVo.playersNumber++;
		}
		/**
		 *删除一个球员  
		 * 
		 */
		public function removePlayerVo($id:int):void
		{
			var goods :PlayerVo
			var len:int = _playerList.length;
			for(var i:int = 0;i < len;i++)
			{
				goods = _playerList[i];
				if(goods.playerid == $id)
				{
					_playerList.splice(i,1);
					_playerCenterVo.playersNumber --;
					break;
				}
			}
		}

		/**
		 *更新球员Buff 
		 * @param $id
		 * @param $status
		 * 
		 */
		public function updataPlayerBuff($id:int,$status:int):void
		{
			var playerVo:PlayerVo = getplayerVoById($id);
			if(playerVo == null)return;
			playerVo.status = $status;
			playerVo.updataValue();
		}
		
		/**
		 *更换装备 
		 * @param buffer
		 * 
		 */
		public function replaceEquip(buffer:ByteArray):int
		{
			var playerId:int = buffer.readInt();
			
			var playerVo:PlayerVo = getplayerVoById(playerId);
			if(playerVo == null)return 0;
			playerVo.replaceEquip(buffer);
			return playerId;
		}
		/**
		 *修改号码 
		 * @param $id
		 * @param $number
		 * 
		 */
		public function changePlayerNumber($id:int,$number:int):void
		{
			var playerId:int = $id;
			var playerVo:PlayerVo = getplayerVoById(playerId);
			if(playerVo == null)return ;
			playerVo.number = $number;
		}
		/**
		 *球员列表 
		 */
		public function get playerList():Array
		{
			return _playerList;
		}

		/**
		 *球员中心信息 
		 */
		public function get playerCenterVo():PlayerCenterInfoVo
		{
			return _playerCenterVo;
		}

		/**
		 *第一阵容 
		 */
		public function get firstBattlearrayVo():BattlearrayVo
		{
			return _firstBattlearrayVo;
		}

		/**
		 *第二阵容  
		 */
		public function get secondBattlearrayVo():BattlearrayVo
		{
			return _secondBattlearrayVo;
		}

		/**
		 *球员是否在阵容中 一 、二阵容
		 * @param $id
		 * @return 
		 * 
		 */
		public function playerIsBattlearray($id:int):Boolean
		{
			var arr:Array = _firstBattlearrayVo.battlearrayList;
			var vo:PlayerPlaceVo
			for each(vo in arr)
			{
				if(vo.playerid == $id)
					return true;
			}
			arr = _secondBattlearrayVo.battlearrayList;
			for each(vo in arr)
			{
				if(vo.playerid == $id)
					return true;
			}
			return false;
		}
		/**
		 * 第一阵容球员排序 列表前11名显示首发阵容中的球员；
		 * 排序规则：球员位置>球员品质>等级>球衣号码
		 * 
		 */
		public function firstBattlearraySortOn($formation:int = PlayerCenterDataConstant.FIRSTBATTLEARRAY):void
		{
			var arr:Array = $formation ==PlayerCenterDataConstant.FIRSTBATTLEARRAY? firstBattlearrayVo.battlearrayList:secondBattlearrayVo.battlearrayList;
			var playerLen:int = _playerList.length;
			var battlearrayLen:int = arr.length;
			for(var i:int = 0;i < playerLen;i++)
			{
				for(var j:int = 0;j < battlearrayLen;j++)
				{
					if(_playerList[i].playerid == arr[j].playerid)
					{
						_playerList[i].formation = 1;	
						break;
					}
					_playerList[i].formation = 0;
				}
			}
			_playerList.sortOn(["formation","place","color","number"],[Array.DESCENDING,Array.NUMERIC,Array.DESCENDING,Array.NUMERIC]);
		}
		/**
		 *更新球员体力 
		 * id为0则增加所有球员的体力
		 * id不为0 则增加单个球员的体力
		 * @param $id
		 * @param $power
		 * 
		 */
		public function updataPlayerPower($id:int=0,$power:int = 0):void
		{
			if($id == 0)
			{
				for each(var goods :PlayerVo in _playerList)
				{
					goods.physical = goods.maxphysical;
				}
				return;
			}
			
			
			var playerVo:PlayerVo = getplayerVoById($id);
			if(playerVo == null)return;
			playerVo.physical = $power;
			if(playerVo.physical <=0)
				playerVo.physical = 0;
		}
		/**
		 *更新球员体力 
		 * 每小时增加所有球员一点体力
		 */
		public function updataPlayerphysical():void
		{
			var h:int = AppData.getInstance().clientHourse;
			if( h- _playerCenterVo.updataphysicalTime < 0)
				h += 23 ;
			var temp:int = h - _playerCenterVo.updataphysicalTime;
			if( temp > 0)
			{
				for each(var goods :PlayerVo in _playerList)
				{
					goods.updatapphysical(temp);
				}
				_playerCenterVo.updataphysicalTime = AppData.getInstance().clientHourse;
			}
			
		}
		
		/**
		 *减少阵容中的球员体力 (减少一点)
		 * 比赛后减少阵容中的球员体力
		 */
		public function reducePlayerPhysical():void
		{
			var arr:Array = _firstBattlearrayVo.battlearrayList;
			for each(var vo:PlayerPlaceVo in arr)
			{
				var playerVo:PlayerVo = getplayerVoById(vo.playerid);
				if(playerVo == null)continue;
				playerVo.updatapphysical(-1);
			}
			
		}
		/**
		 *更新球员体力 
		 * @param $obj
		 * 
		 */
		public function updatePlayerPhysical($arr:Array):void
		{
			for each(var p :PlayerVo in _playerList)
			{
				for each(var obj:Object in $arr)
				{
					if(p.playerid == obj.playerid)
					{
						p.physical = obj.physical;
						p.status = obj.status;
						break;
					}
				}
			}
			_playerCenterVo.updataphysicalTime = AppData.getInstance().clientHourse;
		}
		
		public function get isUpdatePlayerPhysical():Boolean
		{
			
			return false;
		}
//		/**
//		 * 球员中心升级增加球员体力
//		 * 3 ，6， 9 
//		 * 
//		 */
//		public function playerCenterUpUpdataPlayerphysical():void
//		{
//			for each(var goods :PlayerVo in _playerList)
//			{
//				goods.maxphysical++;
//			}
//		}
		
		/**
		 *解析xml 球员坐标
		 * 
		 */		
		private function analysisTactics():void
		{
			if(_tacticsDic == null)
			{
				_tacticsDic = new Dictionary();
				var tracticsVo:TrainingTacticsVo;
				var resConfig:XML = ResourceManager.instance.getResource(ResourceConst.TRAINING_TB_TACTICS);
				var xmlList:XMLList =  resConfig.children();
				for each (var xml:XML in xmlList)
				{
					tracticsVo = new TrainingTacticsVo();
					tracticsVo.analysisData(xml);
					_tacticsDic[tracticsVo.tid] = tracticsVo;
//					_lineUpNumber++;
				}
			}
			
		}
		
		
		/**
		 *获取默认阵型 
		 * 
		 */
		public function getdefaultBattlearray($id:int):Array
		{
			analysisTactics();
			var defaultBattlearrayVo:TrainingTacticsVo = _tacticsDic[$id];//获取默认阵型位置
			return GetDefaultBattlearray.defaultBattlearray(defaultBattlearrayVo,_playerList);
		}
		
		/**
		 *获取球员列表
		 * 转换为PlayerStarVo后 
		 * @return 
		 * 
		 */		
		public function getPlayerStarList():Array
		{
			var res :Array = [];
			for each(var p :PlayerVo in playerList)
			{
				res.push(p.copyToStarVo());
			}
			return res;
		}
		/**
		 *获取自己
		 * 转换为 PlayerStarVo
		 * @return 
		 * 
		 */		
		public function getSelfPlayerStar():PlayerStarVo
		{
			for each(var p :PlayerVo in playerList)
			{
				if(p.self == 1)
				{
					return p.copyToStarVo();
				}
			}
			return null;
		}
		/**
		 *把球员初始化为1级属性 
		 * @param playerId
		 * 
		 */		
		public function initPlayerInfo(playerId :int):void
		{
			var pvo :PlayerVo = getplayerVoById(playerId);
			if(pvo)
			{
				pvo.toOneLevInfo();
			}
		}
		/**
		 *获取自己角色球员信息 
		 * @return 
		 * 
		 */		
		public function getSelfInfo():PlayerVo
		{
			for each(var p :PlayerVo in playerList)
			{
				if(p.self == 1)
				{
					return p;
				}
			}
			return null;
		}
		/**
		 *场上球员是否有受伤 
		 * @return true在场上的球员有受伤，false则没有受伤
		 * 您当前的队伍中有球员受伤，会大大影响球队的实力！\n您可以在球员中心治疗受伤的球员！
		 */		
		public function get fieldisinjured():Boolean
		{
			var arr:Array = firstBattlearrayVo.battlearrayList;
			for each(var vo:PlayerPlaceVo in arr)
			{
				var playerVo:PlayerVo = getPlaceVo(vo.playerid);
				if(playerVo == null)continue;
				if(playerVo.status == 2)
				{
					return true;
				}
			}
			
			return false;
		}
		/**
		 * 球员受伤数量达到一定后，不可以进行比赛
		 * 
		 * 您的上场球员已经有5名受伤，球员们联合起来罢赛了！" +
					"他们抗议如此赤果果的压榨行为并且上报国际足联，老板，赶快去给他们疗伤吧~
		 * @return true 则不可以比赛，false没达到受伤的数量，可以进行比赛 
		 * 
		 */
		public function get fieldPlayerinjuredNum():Boolean
		{
			var num:int;
			var arr:Array = firstBattlearrayVo.battlearrayList;
			for each(var vo:PlayerPlaceVo in arr)
			{
				var playerVo:PlayerVo = getPlaceVo(vo.playerid);
				if(playerVo == null)continue;
				if(playerVo.status == 2)
				{
					num++;
				}
			}
			if(INJURED <= num)return true;
			return false;
		}
		/**
		 *获取场上的球员 
		 * 
		 */
		public function getPlaceVo($id:int):PlayerVo
		{
			for each(var vo:PlayerVo in playerList)
			{
				if(vo.playerid == $id)
					return vo;
			}
			return null;
			
		}
	}
}